package com.realdolmen.jcc.mobileSquad.service.impl
{
	import com.realdolmen.jcc.mobileSquad.dao.CustomerDao;
	import com.realdolmen.jcc.mobileSquad.dao.impl.CustomerDaoImpl;
	import com.realdolmen.jcc.mobileSquad.domain.Customer;
	import com.realdolmen.jcc.mobileSquad.service.CustomerService;
	
	import mx.collections.ArrayCollection;
	
	import org.as3commons.lang.StringUtils;
	
	/**
	 * Implementation of the CustomerService Interface.
	 * 
	 * The reason for this singleton class is that we need a reference to this class in the views.
	 * To save memory Views are destroyd when they are not visible.
	 * This will lead to database problems.
	 * @author DOMSIEBE
	 */	
	public class CustomerServiceImpl implements CustomerService
	{
		private var customerDao:CustomerDao;
		private static var instance:CustomerServiceImpl;
		
		[Bindable] public var customers:ArrayCollection;
		
		/**
		 * Get an instance of the CustomerService
		 * @return an instance of the CustomerService.
		 */		
		public static function getInstance():CustomerService{
			if (instance == null){
				instance = new CustomerServiceImpl();
				instance.customerDao = new CustomerDaoImpl();
			}
			return instance;
		}
		
		/**
		 * @InheriteDoc
		 */		
		public function populate():void{
			customerDao.populate();
		}
		
		/**
		 * @InheriteDoc
		 */		
		public function save(customer:Customer):void{
			customerDao.save(customer);
		}
		
		/**
		 * @InheriteDoc
		 */		
		public function update(customer:Customer):void{
			customerDao.update(customer);
		}
		
		/**
		 * @InheriteDoc
		 */		
		public function remove(customer:Customer):void{
			customerDao.remove(customer);
		}
		
		/**
		 * @InheriteDoc
		 */		
		public function next(customer:Customer):Customer{
			var i:int = 0;
			for each(var customerFromList:Customer in customers){
				if(customer.id==customerFromList.id){
					if(i < customers.length-1){
						return customers.getItemAt(i+1) as Customer;
					}
				}
				i++;
			}
			return null;
		}
		
		/**
		 * @InheriteDoc
		 */			
		public function previous(customer:Customer):Customer{
			var i:int = 0;
			for each(var customerFromList:Customer in customers){
				if(customer.id==customerFromList.id){
					if(i > 0){
						return customers.getItemAt(i-1) as Customer;
					}
				}
				i++;
			}
			return null;
		}
		
		/**
		 * @InheriteDoc
		 */		
		public function findAll():void{
			customers = customerDao.findAll();
		}
		
		/**
		 * @InheriteDoc
		 */		
		public function search(companyName:String):void{
			if(!StringUtils.isEmpty(companyName)){
				customers = customerDao.search(companyName);
			}else{
				customers = customerDao.findAll();
			}
		}
		
		/**
		 * @InheriteDoc
		 */	
		public function completeCustomer(customer:Customer):void{
			switch(customer.name){
				case "Base":{
					customer.address.street = "Bevelsesteenweg 42/A";
					customer.address.city = "Nijlen";
					customer.logo = "logo/base.jpg";
					break;
				}
				case "Mobistar":{
					customer.address.street = "Boulevard Auguste Reyers 70";
					customer.address.city = "Bruxelles";
					customer.logo = "logo/mobistar.jpg";
					break;
				}
			}
		}
	}
}